﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using AAA.Meta.Chart;
using AAA.Base.Util;

namespace AAA.Chart.Indicator
{
    public class PriceVolume : DefaultIndicator
    {
        private const int PEAK_INTERVAL = 10;

        private string _strName;
        private string _strStartDate;
        private string _strEndDate;
        private string _strVolumeName;

        public PriceVolume(string strName, string strStartDate, string strEndDate, string strVolumeName)
        {
            _strName = strName;
            _strVolumeName = strVolumeName;
            if (strEndDate == "-")
                _strEndDate = DateTime.Now.ToString("yyyy/MM/dd") + " 23:59";
            else
                _strEndDate = strEndDate;

            if (NumericHelper.IsNumeric(strStartDate))
            {
                _strStartDate = DateTime.Parse(_strEndDate).AddDays(-int.Parse(strStartDate)).ToString("yyyy/MM/dd") + " 00:00";
            }
            else
            {
                _strStartDate = strStartDate;
            }
           
            XAxis = ChartConstants.X2;
            XDataType = ChartConstants.DATA_TYPE_VALUE;
            ChartStyle = ChartStyleEnum.PriceVolume;
            Scrollable = false;
        }

        private int GetPeak(List<float> lstValue, int iIndex)
        {
            for (int i = 1; i <= PEAK_INTERVAL; i++)
            {
                if ((lstValue[iIndex] < lstValue[iIndex - i]) || (lstValue[iIndex] < lstValue[iIndex + i]))
                    return i - 1;
            }
            return PEAK_INTERVAL + 1;
        }

        public override void Calculate()
        {
            ClearChartText();

            List<float> lstVolume = _strVolumeName == "Volume"
                                        ? DataSource.GetPriceVolume(BaseStudy, _strStartDate, _strEndDate).Volume
                                        : DataSource.GetPriceVolume(BaseStudy, _strStartDate, _strEndDate).TickVolume;

            List<float> lstPrice = DataSource.GetPriceVolume(BaseStudy, _strStartDate, _strEndDate).Price;
            Dictionary<float, float> dicVolume = new Dictionary<float, float>();
            float fLastPrice = DataSource.LastPrice(BaseStudy);
            float fLastVolume = 0;

            for (int i = 0; i < lstPrice.Count; i++)
            {
                dicVolume.Add(lstPrice[i], lstVolume[i]);
                if (lstPrice[i] == fLastPrice)
                    fLastVolume = lstVolume[i];
            }
            lstPrice.Sort();
            lstVolume = new List<float>();
            for(int i = 0; i < lstPrice.Count; i++)
                lstVolume.Add(dicVolume[lstPrice[i]]);

            
            for (int i = PEAK_INTERVAL; i < lstPrice.Count - PEAK_INTERVAL; i++)
            {
                if (GetPeak(lstVolume, i) >= PEAK_INTERVAL)
                    DrawText(lstVolume[i], 
                             lstPrice[i], 
                             lstPrice[i].ToString("0"), 
                             fLastPrice < lstPrice[i] ? Color.Pink : Color.Cyan);
            }

            DrawText(fLastVolume,
                     fLastPrice,
                     fLastPrice.ToString("0"), 
                     Color.Red);


            Plot(lstVolume,
                 lstPrice, 
                 _strName, Color.Yellow, Color.Yellow, 1);
        }
    }
}
